﻿Public Class ProductDetail
    Inherits ItemDetailPage

    Protected Property ProductID() As ULong

    Protected Overrides ReadOnly Property InsertionPermitted() As Boolean
        Get
            Return Roles.IsUserInRole("Manager") Or Roles.IsUserInRole("Editor")
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ULong.TryParse(Request.QueryString.Get("ID"), ProductID)

        If Not IsPostBack Then
            If Not ProductID AndAlso Request.QueryString.Get("Action") = "Add" Then
                If InsertionPermitted Then
                    DetailFormView.DefaultMode = FormViewMode.Insert
                Else
                    StopEditing()
                End If
            ElseIf ProductID AndAlso ProductExists(ProductID) Then
                If Request.QueryString.Get("Action") = "Edit" Then
                    If ModificationPermitted Then
                        DetailFormView.DefaultMode = FormViewMode.Edit
                        UpdateDataSource()
                    Else
                        StopEditing()
                    End If
                Else
                    DetailFormView.DefaultMode = FormViewMode.ReadOnly
                    UpdateDataSource()
                End If
            Else
                RedirectToList()
            End If
        End If
    End Sub

    Protected Sub SaveButton_Click(sender As Object, e As EventArgs)
        If ModificationPermitted Then
            Dim gateway As New ProductGateway()
            Dim model As TextBox = DetailFormView.FindControl("ModelTextBox")
            Dim manufacturer As TextBox = DetailFormView.FindControl("ManufacturerTextBox")
            Dim website As TextBox = DetailFormView.FindControl("WebsiteTextBox")
            Dim length As TextBox = DetailFormView.FindControl("LengthTextBox")
            Dim height As TextBox = DetailFormView.FindControl("HeightTextBox")
            Dim width As TextBox = DetailFormView.FindControl("WidthTextBox")
            gateway.Update(ProductID, model.Text, manufacturer.Text, website.Text,
                           CUInt(length.Text), CUInt(height.Text), CUInt(width.Text))
            StopEditing()
        End If
    End Sub

    Protected Sub CancelEditButton_Click(sender As Object, e As EventArgs)
        StopEditing()
    End Sub

    Protected Sub EditButton_Click(sender As Object, e As EventArgs)
        StartEditing()
    End Sub

    Protected Sub DeleteButton_Click(sender As Object, e As EventArgs)
        If ModificationPermitted Then
            Dim gateway As New ProductGateway()
            gateway.DeleteByID(ProductID)
            RedirectToList()
        End If
    End Sub

    Protected Sub SubmitButton_Click(sender As Object, e As EventArgs)
        If InsertionPermitted Then
            Dim gateway As New ProductGateway()
            Dim model As TextBox = DetailFormView.FindControl("ModelTextBox")
            Dim manufacturer As TextBox = DetailFormView.FindControl("ManufacturerTextBox")
            Dim website As TextBox = DetailFormView.FindControl("WebsiteTextBox")
            Dim length As TextBox = DetailFormView.FindControl("LengthTextBox")
            Dim height As TextBox = DetailFormView.FindControl("HeightTextBox")
            Dim width As TextBox = DetailFormView.FindControl("WidthTextBox")
            ProductID = gateway.Insert(model.Text, manufacturer.Text, website.Text,
                                       CUInt(length.Text), CUInt(height.Text), CUInt(width.Text),
                                       GetCurrentUserID())
            StopEditing()
        End If
    End Sub

    Protected Function ProductExists(ByVal ID As ULong) As Boolean
        Dim gateway As New ProductGateway()
        Return gateway.Exists(ID)
    End Function

    Protected Overrides ReadOnly Property UserHasOwnership() As Boolean
        Get
            Dim gateway As New ProductGateway()
            Return gateway.HasOwnership(ProductID, User.Identity.Name)
        End Get
    End Property

    Protected Sub StartEditing()
        Dim redirectUrl As String = String.Format("~/Pages/ProductDetail.aspx?ID={0}&Action=Edit", ProductID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Sub StopEditing()
        Dim redirectUrl As String = String.Format("~/Pages/ProductDetail.aspx?ID={0}", ProductID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Sub RedirectToList()
        Dim redirectUrl As String = String.Format("~/Pages/ProductList.aspx")
        Response.Redirect(redirectUrl)
    End Sub

    Protected Function getPhotoPath(ByVal fileName As String) As String
        Return PhotoUtils.getNormalPath(fileName)
    End Function

    Protected Sub UpdateDataSource()
        Dim gateway As New ProductGateway()
        DetailFormView.DataSource = gateway.FindByID(ProductID)
        DetailFormView.DataBind()
    End Sub
End Class